#include "stdafx.h"
#include "ReferenceCountAdvice.h"
#include "ComObject.h"

typedef struct IUnknownVtbl
{
	BEGIN_INTERFACE

		HRESULT ( STDMETHODCALLTYPE *QueryInterface )(
		IUnknown * This,
		/* [in] */ REFIID riid,
		/* [iid_is][out] */ void **ppvObject);

		ULONG ( STDMETHODCALLTYPE *AddRef )(
			IUnknown * This);

		ULONG ( STDMETHODCALLTYPE *Release )(
			IUnknown * This);

	END_INTERFACE
} IUnknownVtbl;

class CComUnknown: public CComObject<IUnknown,IUnknownVtbl>
{
public:
	CComUnknown(CJoinPoint& joinPoint):CComObject(joinPoint)
	{
	}
	~CComUnknown()
	{
	}

public:
	ULONG STDMETHODCALLTYPE AddRef()
	{
		return pDispatchVtbl->AddRef(pDispatch);
	}

	ULONG STDMETHODCALLTYPE Release()
	{
		return pDispatchVtbl->Release(pDispatch);
	}
};

void CReferenceCountAdvice::Before(CJoinPoint& joinPoint)
{
	CComUnknown unknown(joinPoint);
	unknown.AddRef();
}

void CReferenceCountAdvice::AfterReturning(CJoinPoint& joinPoint,int returnValue)
{
	CComUnknown unknown(joinPoint);
	unknown.Release();
}

static CReferenceCountAdvice _gReferenceCountAdvice;
